package Engine.RMI
{
	import Engine.flash.*;
	import Framework.Protocol.*;
	
	public class Communicator extends EventSender
	{
		private var _connecting:Boolean;
		private var _connected:Boolean;
		private var _url:String;
		private var _connection:RMIConnection;
		private var _session:Session;
		private var _sessionEvent:IRMISessionEvent;
		private var _prepareCommandHandler:IRMIPrepareCommandHandler;
		
		public function Communicator(protocol:IProtocol)
		{
			this._connecting = false;
			this._connected = false;
			this._connection = new RMIConnection(protocol);
		}
		
		public function connect() : Boolean
		{
			var array:Array;
			var ip:String;
			var port:int;
			this._connecting = true;
			try
			{
				this._connection.session = this.session;
				array = this.url.split(":");
				if (array.length == 2)
				{
					ip = array[0];
					port = parseInt(array[1]);
				}
				else
				{
					return false;
				}
				this.connection.connect(ip, port);
				return true;
			}
			catch (ex:Error)
			{
				_connecting = false;
				trace("Communicator::connect.connect error");
			}
			return false;
		}
		
		public function set connecting(connecting:Boolean) : void
		{
			this._connecting = connecting;
		}
		
		public function get connecting() : Boolean
		{
			return this._connecting;
		}
		
		public function set connected(param1:Boolean) : void
		{
			this._connected = param1;
		}
		
		public function get connected() : Boolean
		{
			return this._connected;
		}
		
		public function set url(url:String) : void
		{
			this._url = url;
		}
		
		public function get url() : String
		{
			return this._url;
		}
		
		public function get connection() : RMIConnection
		{
			return this._connection;
		}
		
		public function set session(session:Session) : void
		{
			this._session = session;
		}
		
		public function get session() : Session
		{
			return this._session;
		}
		
		public function set sessionEvent(event:IRMISessionEvent) : void
		{
			this._sessionEvent = event;
			
		}
		
		public function get sessionEvent() : IRMISessionEvent
		{
			return this._sessionEvent;
		}
		
		public function set prepareCommandHandler(handler:IRMIPrepareCommandHandler) : void
		{
			this._prepareCommandHandler = handler;
		}
		
		public function get prepareCommandHandler() : IRMIPrepareCommandHandler
		{
			return this._prepareCommandHandler;
		}
	}
}
